package com.sgb.kjwl.view.ui.fragment.packers;

import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrPackersPeopleBinding;
import com.sgb.kjwl.di.packers.DaggerPackersFragmentComponent;
import com.sgb.kjwl.di.packers.PackersFragmentModule;
import com.sgb.kjwl.model.entity.FindPeopleEntity;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TeamPriceItemEntity;
import com.sgb.kjwl.model.entity.WorkYear;
import com.sgb.kjwl.model.entity.WorkerLevel;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.packers.FindPeopleAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.sgb.kjwl.viewmodel.packers.FragFindPeopleModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class PackersPeopleFragment extends AppLazyFragment {

    private FrPackersPeopleBinding binding;

    @Inject
    FragFindPeopleModel viewModel;
    FindPeopleAdapter findPeopleAdapter;

    private PickPopWindow workerYearPopWindow;
    private PickPopWindow workYearPopWindow;
    private PickPopWindow workTypePopWindow;
    private PickPopWindow workAreaPopWindow;

    private PickSelectEntity all;

    private List<PickSelectEntity> workerLevelData = new ArrayList<>();//工人级别
    private List<PickSelectEntity> workerYearData = new ArrayList<>();//工人年龄
    private List<PickSelectEntity> workTypeData = new ArrayList<>();//工种
    private List<PickSelectEntity> workYearData = new ArrayList<>();//工龄
    private List<PickSelectEntity> areaData = new ArrayList<>();//所在地区

    private PickResultEntity pickResultWorkType;
    private PickResultEntity pickResultWorkArea;
    private PickResultEntity pickResultWorkerYear;
    private PickResultEntity pickResultWorkYear;

    private String workerLevelNo = "";

    private Drawable img_up;
    private Drawable img_down;
    private Drawable img_select_down;

    int anInt;

    public static PackersPeopleFragment newInstance(int position) {
        PackersPeopleFragment fragment = new PackersPeopleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("TYPE_INDEX", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPackersFragmentComponent
                .builder()
                .appComponent(appComponent)
                .packersFragmentModule(new PackersFragmentModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_packers_people, container, false);
        return binding.getRoot();
    }

    private void initFilterData(boolean isLoadMore) {
        if (anInt == 1) {
            viewModel.getClassLeaderList(pickResultWorkArea.getNo(), pickResultWorkYear.getNo(), pickResultWorkType.getNo(), workerLevelNo, pickResultWorkerYear.getNo(), isLoadMore);
        } else {
            viewModel.getNewList(pickResultWorkArea.getNo(), pickResultWorkYear.getNo() , pickResultWorkType.getNo(),  workerLevelNo, pickResultWorkerYear.getNo(), isLoadMore);
        }
    }

    @Override
    protected void initView() {
        binding.setViewModel(viewModel);
        binding.tvWorkerYear.setOnClickListener(this);
        binding.tvWorkType.setOnClickListener(this);
        binding.tvWorkYear.setOnClickListener(this);
        binding.tvWorkArea.setOnClickListener(this);

        assert getArguments() != null;
        anInt = getArguments().getInt("TYPE_INDEX", 0); // 1 为班组长，item 显示班组人数

        if (findPeopleAdapter == null) {
            findPeopleAdapter = new FindPeopleAdapter(getActivity(), null, Glide.with(this));
        }

        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");

        binding.recyclerView.setLayoutManager(new XLinearLayoutManager(getActivity()));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp5)));
        binding.recyclerView.setAdapter(findPeopleAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        Resources res = getResources();
        img_up = res.getDrawable(R.mipmap.ic_up_arrow);
        img_up.setBounds(0, 0, img_up.getMinimumWidth(), img_up.getMinimumHeight());
        Resources res2 = getResources();
        img_select_down = res2.getDrawable(R.mipmap.ic_down_arrow);
        img_select_down.setBounds(0, 0, img_select_down.getMinimumWidth(), img_select_down.getMinimumHeight());
        Resources res3 = getResources();
        img_down = res3.getDrawable(R.mipmap.ic_down_arrow_n);
        img_down.setBounds(0, 0, img_down.getMinimumWidth(), img_down.getMinimumHeight());

        pickResultWorkType = new PickResultEntity();
        pickResultWorkArea = new PickResultEntity();
        pickResultWorkerYear = new PickResultEntity();
        pickResultWorkYear = new PickResultEntity();

        init();
    }

    @Override
    protected void onLazyLoad() {
        initFilterData(false);
        viewModel.getWorkerLevel();
        viewModel.getWorkType();
        viewModel.getWorkYear();
        viewModel.getWorkerYear();
        viewModel.getAddressModel();
    }

    protected void init() {
        //年龄
        workerYearPopWindow = new PickPopWindow(getContext());
        workerYearPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                pickResultWorkerYear.setNo((String) key1);
                pickResultWorkerYear.setName((String) key2);
                pickResultWorkerYear.setPosition((int) key3);
                if (TextUtils.isEmpty((String) key1) || TextUtils.equals("全部", (String) key2)) {
                    binding.tvWorkerYear.setText("年龄");
                } else {
                    binding.tvWorkerYear.setText((String) key2);
                }
                initFilterData(false);
            }
        });
        workerYearPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (TextUtils.isEmpty(pickResultWorkerYear.getNo())) {
                    binding.tvWorkerYear.setTextColor(getResources().getColor(R.color.color_666666));
                    binding.tvWorkerYear.setCompoundDrawables(null, null, img_down, null);
                } else {
                    binding.tvWorkerYear.setTextColor(getResources().getColor(R.color.new_blue));
                    binding.tvWorkerYear.setCompoundDrawables(null, null, img_select_down, null);
                }
            }
        });

        //工种
        workTypePopWindow = new PickPopWindow(getContext());
        workTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                pickResultWorkType.setNo((String) key1);
                pickResultWorkType.setName((String) key2);
                pickResultWorkType.setPosition((int) key3);
                if (TextUtils.isEmpty((String) key1) || TextUtils.equals("全部", (String) key2)) {
                    binding.tvWorkType.setText("工种");
                } else {
                    binding.tvWorkType.setText((String) key2);
                }
                initFilterData(false);
            }
        });
        workTypePopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (TextUtils.isEmpty(pickResultWorkType.getNo())) {
                    binding.tvWorkType.setTextColor(getResources().getColor(R.color.color_666666));
                    binding.tvWorkType.setCompoundDrawables(null, null, img_down, null);
                } else {
                    binding.tvWorkType.setTextColor(getResources().getColor(R.color.new_blue));
                    binding.tvWorkType.setCompoundDrawables(null, null, img_select_down, null);
                }
            }
        });

        //工龄
        workYearPopWindow = new PickPopWindow(getContext());
        workYearPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                pickResultWorkYear.setNo((String) key1);
                pickResultWorkYear.setName((String) key2);
                pickResultWorkYear.setPosition((int) key3);
                if (TextUtils.isEmpty((String) key1) || TextUtils.equals("全部", (String) key2)) {
                    binding.tvWorkYear.setText("工龄");
                } else {
                    binding.tvWorkYear.setText((String) key2);
                }
                initFilterData(false);
            }
        });
        workYearPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (TextUtils.isEmpty(pickResultWorkYear.getNo())) {
                    binding.tvWorkYear.setTextColor(getResources().getColor(R.color.color_666666));
                    binding.tvWorkYear.setCompoundDrawables(null, null, img_down, null);
                } else {
                    binding.tvWorkYear.setTextColor(getResources().getColor(R.color.new_blue));
                    binding.tvWorkYear.setCompoundDrawables(null, null, img_select_down, null);
                }
            }
        });

        //所在区域
        workAreaPopWindow = new PickPopWindow(getContext());
        workAreaPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                pickResultWorkArea.setNo((String) key1);
                pickResultWorkArea.setName((String) key2);
                pickResultWorkArea.setPosition((int) key3);
                if (TextUtils.isEmpty((String) key1) || TextUtils.equals("全部", (String) key2)) {
                    binding.tvWorkArea.setText("所在区域");
                } else {
                    binding.tvWorkArea.setText((String) key2);
                }
                initFilterData(false);
            }
        });
        workAreaPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (TextUtils.isEmpty(pickResultWorkArea.getNo())) {
                    binding.tvWorkArea.setTextColor(getResources().getColor(R.color.color_666666));
                    binding.tvWorkArea.setCompoundDrawables(null, null, img_down, null);
                } else {
                    binding.tvWorkArea.setTextColor(getResources().getColor(R.color.new_blue));
                    binding.tvWorkArea.setCompoundDrawables(null, null, img_select_down, null);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_work_area:
                setTextViewChange(binding.tvWorkArea, R.mipmap.ic_up_arrow, R.color.new_blue);
                showWorkAreaWindow();
                break;
            case R.id.tv_work_type:
                setTextViewChange(binding.tvWorkType, R.mipmap.ic_up_arrow, R.color.new_blue);
                showWorkTypeWindow();
                break;
            case R.id.tv_work_year:
                setTextViewChange(binding.tvWorkYear, R.mipmap.ic_up_arrow, R.color.new_blue);
                showWorkYearWindow();
                break;
            case R.id.tv_worker_year:
                setTextViewChange(binding.tvWorkerYear, R.mipmap.ic_up_arrow, R.color.new_blue);
                showWorkerYearPopWindow();
                break;
        }
    }

    public void showList(List<FindPeopleEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                findPeopleAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                findPeopleAdapter.setDatas(list);
            } else {
                findPeopleAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }

        findPeopleAdapter.setOnItemClick(new AdapterOnItemClick<FindPeopleEntity>() {
            @Override
            public void onItemClick(FindPeopleEntity findPeopleEntity, int position) {
                if (anInt == 0)
                    viewModel.getH5Url(findPeopleEntity.getNo());
                if (anInt == 1){
                    viewModel.getClassLeaderH5Url(findPeopleEntity.getNo());
                }
            }
        });
    }

    //工人级别
    public void setWorkerLevelData(List<PickSelectEntity> data) {
        workerLevelData.clear();
        data.add(0, all);
        workerLevelData.addAll(data);
        initTab(data);
    }

    private void initTab(List<PickSelectEntity> data) {
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setCustomView(R.layout.item_tablayout_view_2));
            TextView tvTab = binding.tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_item);
            tvTab.setTextColor(ContextCompat.getColor(getContext(), R.color.color_666666));
            tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tvTab.setText(data.get(i).getProjectscale());
            if (i == 0) {
                tvTab.setTextColor(ContextCompat.getColor(getContext(), R.color.new_blue));
                tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
        }
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
                tv.setTextColor(ContextCompat.getColor(getContext(), R.color.new_blue));
                tv.invalidate();
                workerLevelNo = data.get(tab.getPosition()).getNo();
                initFilterData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tv.setTextColor(ContextCompat.getColor(getContext(), R.color.color_666666));
                tv.invalidate();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //工人年龄
    public void setWorkerYearData(List<PickSelectEntity> datas) {
        workerYearData.clear();
        workerYearData.addAll(datas);
    }

    //工种
    public void setWorkTypeData(List<PickSelectEntity> datas) {
        workTypeData.clear();
        datas.add(0, all);
        workTypeData.addAll(datas);
    }

    //工龄
    public void setWorkYearData(List<PickSelectEntity> datas) {
        workYearData.clear();
        workYearData.addAll(datas);
    }

    //所在地区
    public void setAreaData(List<PickSelectEntity> datas) {
        areaData.clear();
        datas.add(0, all);
        areaData.addAll(datas);
    }

    //工人年龄pop
    private void showWorkerYearPopWindow() {
        workerYearPopWindow.showPopwindow(binding.tvWorkerYear);
        workerYearPopWindow.setData(workerYearData, pickResultWorkerYear.getPosition());
    }

    //工种pop
    private void showWorkTypeWindow() {
        workTypePopWindow.showPopwindow(binding.tvWorkType);
        workTypePopWindow.setData(workTypeData, pickResultWorkType.getPosition());
    }

    //工龄pop
    private void showWorkYearWindow() {
        workYearPopWindow.showPopwindow(binding.tvWorkYear);
        workYearPopWindow.setData(workYearData, pickResultWorkYear.getPosition());
    }

    //所在地区pop
    private void showWorkAreaWindow() {
        workAreaPopWindow.showPopwindow(binding.tvWorkArea);
        workAreaPopWindow.setData(areaData, pickResultWorkArea.getPosition());
    }

    private void setTextViewChange(TextView textView, int img, int textColor) {
        Resources res = getResources();
        Drawable imgs = res.getDrawable(img);
        imgs.setBounds(0, 0, imgs.getMinimumWidth(), imgs.getMinimumHeight());
        textView.setCompoundDrawables(null, null, imgs, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    public void getUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }
}
